//
//  TouchExplorerViewController.m
//  iPhoneDev01
//
//  Created by smu on 11-7-13.
//  Copyright 2011年 duk. All rights reserved.
//

#import "TouchExplorerViewController.h"


@implementation TouchExplorerViewController
@synthesize messageLabel;
@synthesize tapsLabel;
@synthesize touchesLabel;

- (void)updateLabelsFromTouches:(NSSet *)touches
{
    //touches中每个对象都有一个UITouch事件,该事件表示一个手指正在触摸屏幕
    //如果该触摸是一系列轻击的一部分，则可以通过询问任何UITouch对象来查询轻击数量
    NSUInteger numTaps = [[touches anyObject] tapCount];
    NSString *tapsMessage = [[NSString alloc]
                             initWithFormat:@"%d taps detected", numTaps];
    tapsLabel.text = tapsMessage;
    //按压屏幕手指数量
    NSUInteger numTouches = [touches count];
    NSString *touchMsg = [[NSString alloc] initWithFormat:
                          @"%d touches detected", numTouches];
    touchesLabel.text = touchMsg;
    [touchMsg release];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [messageLabel release];
    [tapsLabel release];
    [touchesLabel release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    self.messageLabel = nil;
    self.tapsLabel = nil;
    self.touchesLabel = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    messageLabel.text = @"Touches Began";
    [self updateLabelsFromTouches:touches];
}
//当发生某些事件，导致手势中断
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    messageLabel.text = @"Touches Cancelled";
    [self updateLabelsFromTouches:touches];
}
//手指离开屏幕
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    messageLabel.text = @"Touches Stopped.";
    [self updateLabelsFromTouches:touches];
}
//当手指移过屏幕
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    messageLabel.text = @"Drag Detected";
    [self updateLabelsFromTouches:touches];
}
@end
